import jwt
import datetime as dt
from jassjwt import JaaSJwtBuilder

"""
RS256.sh
ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key

# Don't add passphrase
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
cat jwtRS256.key
cat jwtRS256.key.pub
"""

def jitsi_opensource_mock():
  PRIVATE_KEY = "/home/tt/key/testkey/jwtRS256.key"
  PUBLIC_KEY = "/home/tt/key/testkey/jwtRS256.key.pub"

  key_content = open(PRIVATE_KEY).read()
  pub_key_content = open(PUBLIC_KEY).read()
  jisit_header = {
    "kid": "jitsi/custom_key_name",
    # "typ": "JWT",
    # "alg": "RS256"
  }
  exp = dt.datetime.utcnow() + dt.timedelta(days=100)
  payload = {
    "context": {
      "user": {
        "avatar": "https:/gravatar.com/avatar/abc123",
        "name": "John Doe",
        "email": "jdoe@example.com",
        "id": "abcd:a1b2c3-d4e5f6-0abc1-23de-abcdef01fedcba"
      },
      "group": "a123-123-456-789"
    },
    "aud": "jitsi",
    "iss": "my_client",
    "sub": "meet.jit.si",
    "room": "*",
    "exp": exp
  }

  token = jwt.encode(algorithm="RS256", key=key_content, headers=jisit_header, payload=payload)
  print(token)

  decoding = jwt.decode(token, key=pub_key_content, algorithms="RS256", audience="jitsi")
  print(decoding)


"""
    appid = "meet-sangatel"
    content = request.json
    username = current_user["username"]
    email = username + "@emeow.com"
    room = content["room"]
    roomname = appid + "/" + room
"""

def jitsi_opensource_mock_shared_secret(room, username):
  key_content = "meow-secrect"
  appid = "meet-sangatel"
  jisit_header = {
    "kid": "jitsi/custom_key_name",
  }
  exp = dt.datetime.utcnow() + dt.timedelta(days=100)
  email = username + "@emeow.com"
  roomname = appid + "/" + room
  payload = {
    "context": {
      "user": {
        "avatar": "https://i.imgur.com/jo6P5S5.jpeg",
        "name": username,
        "email": email,
        "id": "abcd:a1b2c3-d4e5f6-0abc1-23de-abcdef01fedcba"
      },
      "group": "a123-123-456-789"
    },
    "aud": "jitsi",
    "iss": appid,
    "sub": "meet.jit.si",
    "room": roomname,
    "exp": exp
  }

  token = jwt.encode(algorithm="HS256", key=key_content, headers=jisit_header, payload=payload)
  print(token)

  decoding = jwt.decode(token, key=key_content, algorithms="HS256", audience="jitsi")
  print(decoding)

###################
def jaas_8x8():
  PRIVATE_KEY = "/home/tt/key/jitsi/jitsi.private"
  PUBLIC_KEY = "/home/tt/key/jitsi/jitsi.pub"
  key_content = open(PRIVATE_KEY).read()
  pubkey_content = open(PUBLIC_KEY).read()

  jaasJwt = JaaSJwtBuilder()  # Create new JaaSJwtBuilder and setup the claims.

  token = jaasJwt.withDefaults() \
    .withApiKey("vpaas-magic-cookie-2c39fbf723dc401b89e2f666fa7e6ace/cca7c6") \
    .withUserName("ttpro1995") \
    .withUserEmail("ttpro1995@gmail.com") \
    .withModerator(True) \
    .withAppID("vpaas-magic-cookie-2c39fbf723dc401b89e2f666fa7e6ace") \
    .withUserAvatar("https://asda.com/avatar") \
    .signWith(key_content)  # Pass the private here
  print(token)
  print(type(token))
  print(token.decode("utf-8"))
  token_str = token.decode("utf-8")
  decoding = jwt.decode(token_str, key=pubkey_content, algorithms="RS256", audience="jitsi")
  print(decoding)
  return token_str


if __name__ == "__main__":
  jitsi_opensource_mock_shared_secret()